#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 王者荣耀|英雄联盟
import time
import random


class Hero:

    def __init__(self, 名字, 血量, 伤害, 速度, 武器, 头像='🙄'):
        self.名字=名字
        self.血量=血量
        self.伤害=伤害 # 每次打击输出
        self.速度=速度 # 每秒打击次数（概率）
        self.头像=头像
        self.武器=武器

    # def create(s):
    #     pass

    def show(self):
        if self.isdead():
            print('英雄 %s 挂了 x_x' % self.头像)
        else:
            print('英雄 %s 血量：💧 X %d' % (self.头像, 
            self.血量))

    def attack(self, other):
        # self 攻击 other
        if random.random() < self.速度:
            self.attack_anim(other)  # 攻击动画
            other.血量 -= self.伤害

    def __rshift__(self, other):
        # 魔法方法 self >> other 运算符重载
        self.attack(other)

    def __lshift__(self, other):
        # 魔法方法 self << other 运算符重载
        other.attack(self)

    def attack_anim(self, other):
        # 攻击动画
        print('[英雄 %s 攻击 英雄 %s]' % (self.头像, other.头像))
        time.sleep(0.03)
        for k in range(10):
            print('%s %s %s'%(self.头像, self.武器*k, other.头像))
            time.sleep(0.03)
            print('\n'*5)

    def isdead(self):
        return self.血量<=0

hero_dict = {'亚瑟':Hero('亚瑟', 20,1,.7, '🔫', '🦖'),
'吕布':Hero('吕布', 20, 3,.5, '🗡', '👨'),
'貂蝉':Hero('貂蝉', 20, 5,.3, '🛡', '👧'),
'无名氏':Hero('无名氏', 50, 5,.1, '💣', '🐻')}


class Army:
    def __init__(self, name, heros, 头像='🙄'):
        # 初始化方法
        self.name = name
        self.heros = heros
        self.头像 = 头像

    def create(s, 头像='🙄'):
        # 静态方法
        # 如 s='信计18 亚瑟 吕布'
        l = s.split()
        name = l[0]
        heros = [hero_dict[x] for x in l[1:]]
        return Army(name, heros, 头像)

    def choose(self):
        # 普通方法
        # 随机选择活的英雄
        return random.choice([hero for hero in self.heros
                    if not hero.isdead()])

    def attack(self, other):
        self.attack_anim(other)
        hero1 = self.choose()
        hero2 = other.choose()
        hero1 >> hero2

    def __rshift__(self, other):
        # 魔法方法 self >> other 运算符重载
        self.attack(other)

    def __lshift__(self, other):
        # 魔法方法 self << other 运算符重载
        other.attack(self)

    def attack_anim(self, other):
        print(self.name, '攻击', other.name)

    def isdead(self):
        # 列表推导
        return all([hero.isdead() for hero in self.heros])

    def show(self):
        print('军队<%s>:' % self.name)
        for hero in self.heros:
            hero.show()


def main():
    """史诗级游戏，天王巨星张家辉代言
    震撼上市。
    """
    army1 = Army.create('信计18 亚瑟 吕布', '⛴') # 用户
    army2 = Army.create('信计19 貂蝉 无名氏', '🚘')

    round = 100
    for _ in range(round):
        time.sleep(1)
        army1 >> army2
        army1 << army2
        if army1.isdead():
            print('军队', army1.头像, '挂了')
            break
        if army2.isdead():
            print('军队', army2.头像, '挂了')
            break
        army1.show(); army2.show()

main()
